<template>
  <el-card>
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="add()" v-auth="auths.add">
          {{ $t('operation.add') }}
        </el-button>
      </el-col>
      <el-col :span="12"></el-col>
    </el-row>
    <el-table border style="margin: 10px 0px" :data="list">
      <el-table-column
        :label="$t('sys.username')"
        show-overflow-tooltip
        prop="username"
      ></el-table-column>
      <el-table-column
        :label="$t('sys.nickName')"
        show-overflow-tooltip
        prop="nickName"
      ></el-table-column>
      <el-table-column
        :label="$t('sys.desc')"
        show-overflow-tooltip
        prop="description"
      ></el-table-column>
      <el-table-column
        :label="$t('operation.createTime')"
        show-overflow-tooltip
        prop="createTime"
      ></el-table-column>
      <!-- <el-table-column label="图片" width="150px">
                <template #="{ row, $index }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px;">
                </template>
            </el-table-column> -->
      <el-table-column :label="$t('operation.oper')" width="250px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            v-auth="auths.edit"
            @click="add(row)"
          ></el-button>
          <el-popconfirm
            :title="$t('operation.ask')"
            width="200px"
            :confirm-button-text="$t('operation.confirm')"
            :cancel-button-text="$t('operation.cancel')"
            @confirm="removeRow(row.userId)"
          >
            <template #reference>
              <el-button
                type="primary"
                size="small"
                v-auth="auths.del"
                icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getPage"
      @size-change="handler"
    />

    <Save ref="save" @callback="getPage()" />
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
//引入请求
import { userGetPage, userAdd, userEdit, userDelete } from '@/api/system/user'
//引入ts类型
import { ElMessage } from 'element-plus'
import Save from './save.vue'
const auths = {
  add: '/sys/user/add',
  edit: '/sys/user/update',
  del: '/sys/user/batchDelete',
  export: '/sys/user/exportXls',
}
const save = ref()
//分页器当前页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(10)
let total = ref<number>(0)
let list = ref([])
//控制抽屉显示与隐藏的字段
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
//组件挂载完毕
onMounted(() => {
  getPage()
})
const getPage = async (pager = 1) => {
  //当前分页器的页码
  pageNo.value = pager
  let { status, data, msg } = await userGetPage({
    pageNo: pageNo.value,
    pageSize: pageSize.value,
  })
  if (status == 200) {
    let { records, total: tatalNums } = data
    total.value = tatalNums
    list.value = records
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}
//分页器下拉菜单发生变化触发
const handler = (pageSizes: number) => {
  getPage()
}

const add = (row: any) => {
  save.value.show(row)
}

const removeRow = async (id: number) => {
  //删除某一个已有商品的情况
  let result: any = await userDelete([id])
  if (result.status == 200) {
    //提示信息
    ElMessage({ type: 'success', message: '删除成功' })
    //获取已有全部商品
    getPage()
  } else {
    //删除失败
    ElMessage({ type: 'error', message: '系统数据不能删除' })
  }
}
</script>

<style scoped></style>
